package com.hs.jhzd.config;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.web.PageableHandlerMethodArgumentResolver;
import org.springframework.http.MediaType;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
import org.springframework.http.converter.FormHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.ResourceHttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.http.converter.xml.SourceHttpMessageConverter;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

  @Override
  public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/").setViewName("redirect:/conf/admin/main");
  }

  @Override
  public void configureViewResolvers(ViewResolverRegistry registry) {
    registry.jsp("/", ".jsp");
  }

  @Override
  public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
    configurer.enable();
  }

  @Override
  public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    converters.addAll(meesageConverters());
  }

  @Override
  public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
    argumentResolvers.add(new PageableHandlerMethodArgumentResolver());
  }

  @Override
  public void addCorsMappings(CorsRegistry registry) {
    registry.addMapping("/**").allowedHeaders("*").allowedMethods("*").allowedOrigins("*");
  }

  /**
   * restTemplate.
   *
   * @return restTemplate
   */
  @Bean
  public RestTemplate restTemplate() {
    final RestTemplate restTemplate = new RestTemplate(httpRequestFactory());
    restTemplate.setMessageConverters(meesageConverters());
    return restTemplate;
  }

  /**
   * http request factory.
   *
   * @return http request factory
   */
  @Bean
  public SimpleClientHttpRequestFactory httpRequestFactory() {
    SimpleClientHttpRequestFactory simpleClientHttpRequestFactory =
        new SimpleClientHttpRequestFactory();
    simpleClientHttpRequestFactory.setReadTimeout(10000);
    simpleClientHttpRequestFactory.setConnectTimeout(5000);
    return simpleClientHttpRequestFactory;
  }

  private static List<HttpMessageConverter<?>> meesageConverters() {
    List<HttpMessageConverter<?>> converters = new ArrayList<>();

    MappingJackson2HttpMessageConverter jackson2HttpMessageConverter =
        new MappingJackson2HttpMessageConverter();
    jackson2HttpMessageConverter.setSupportedMediaTypes(
        Arrays.asList(MediaType.APPLICATION_JSON_UTF8, MediaType.TEXT_HTML));
    jackson2HttpMessageConverter.setDefaultCharset(Charset.forName("UTF-8"));
    converters.add(jackson2HttpMessageConverter);

    StringHttpMessageConverter stringHttpMessageConverter =
        new StringHttpMessageConverter(Charset.forName("UTF-8"));
    stringHttpMessageConverter.setSupportedMediaTypes(Arrays.asList(MediaType.TEXT_PLAIN));
    converters.add(stringHttpMessageConverter);


    converters.add(new ResourceHttpMessageConverter());
    converters.add(new ByteArrayHttpMessageConverter());
    converters.add(new FormHttpMessageConverter());
    converters.add(new SourceHttpMessageConverter<>());

    return converters;
  }

  /*
   * Configure ContentNegotiationManager
   */
  @Override
  public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
    configurer.ignoreAcceptHeader(true).defaultContentType(MediaType.APPLICATION_JSON_UTF8);
    configurer.mediaType("json", MediaType.APPLICATION_JSON_UTF8);
    configurer.mediaType("bmp", MediaType.APPLICATION_OCTET_STREAM);
    configurer.mediaType("jpg", MediaType.IMAGE_JPEG);
    configurer.mediaType("png", MediaType.IMAGE_PNG);

  }

}
